package com.neusoft.zqygbc.neusoft_wenda.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.activity.LoginActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.NewQuestionActivity;
import com.neusoft.zqygbc.neusoft_wenda.activity.SearchActivity;
import com.neusoft.zqygbc.neusoft_wenda.adapter.QListAdapter;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionList;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionEntity;
import com.neusoft.zqygbc.neusoft_wenda.service.QuestionService;
import com.neusoft.zqygbc.neusoft_wenda.view.SwipyRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Lenovo on 2017/3/13.
 */

public class HomeFragment extends Fragment implements TabLayout.OnTabSelectedListener {

    private SearchView searchView;
    private List<Fragment> list = new ArrayList<>();
    private String title[] = {"全部","Android","PHP","Web","iOS"};
    private ViewPager viewPager;
    private TabLayout tabLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab01, container, false);
        initViews(view,savedInstanceState);
        initEvents();
        return view;
    }

    private void initViews(View view,Bundle savedInstanceState) {
        searchView = (SearchView) view.findViewById(R.id.home_searchview);
        searchView.setIconifiedByDefault(false);
        searchView.setFocusable(false);

        tabLayout = (TabLayout) view.findViewById(R.id.tab_layout_home);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager_home);

        list.add(new AllFragment());
        list.add(new Fragment_1());
        list.add(new Fragment_2());
        list.add(new Fragment_3());
        list.add(new Fragment_4());
        /**
         * 设置TabLayout的模式
         * TabLayout具有两个模式
         *  TabLayout.MODE_FIXED        这个也是默认的模式，tab平分屏幕宽度
         *  TabLayout.MODE_SCROLLABLE   意思为可滚动的，每个tab具有固定的宽度，
         *                              当屏幕容不下时可以左右滑动选择页面
         **/
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //设置Viewpager的预加载的条目 间接保存Fragment的状态
        viewPager.setOffscreenPageLimit(1);
        HomeFragment.MyFragmentPagerAdapter adapter = new HomeFragment.MyFragmentPagerAdapter();
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);

        tabLayout.setTabsFromPagerAdapter(adapter);
        //自定义的tab布局
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            View v = getLayoutInflater(savedInstanceState).inflate(R.layout.tabhost_tab, null);
            TextView textView = (TextView) v.findViewById(R.id.tab_text);
            textView.setText(title[i]);
            //设置自定义的tab布局
            tab.setCustomView(v);
        }
    //    onTabSelected(tabLayout.getTabAt(1));
        onTabSelected(tabLayout.getTabAt(0));

        tabLayout.setOnTabSelectedListener(this);

    }

    private void initEvents() {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition();
        viewPager.setCurrentItem(position, false);

    }


    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter() {
            super(getActivity().getSupportFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}



